package com.elinkway.infinitemovies.lottery;


import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import com.elinkway.infinitemovies.application.MoviesApplication;
import com.elinkway.infinitemovies.bean.Lottery;
import com.elinkway.infinitemovies.bean.LotteryPopWindowInfo;
import com.elinkway.infinitemovies.utils.LogUtils;
import com.elinkway.infinitemovies.utils.Utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class LotteryManager {
    private Context context;
    private static final String LOTTERY = "lottery";
    private static final String TIME_PATTERN = "yyyyMMdd";
    

    private LotteryManager(Context context) {
        this.context = context;
    }

    private static LotteryManager instance =
            new LotteryManager(MoviesApplication.getInstance());

    public static LotteryManager getInstance() {
        return instance;
    }
    public boolean isLotteryDeviceInfoPosted(){
        SharedPreferences sp = context.getSharedPreferences(LOTTERY, Context.MODE_PRIVATE);
        return sp.getBoolean("device_info_posted", false);
    }
    public void setLotteryDeviceInfoPosted(){
        SharedPreferences sp = context.getSharedPreferences(LOTTERY, Context.MODE_PRIVATE);
        sp.edit().putBoolean("device_info_posted", true).commit();
    }
    public boolean isLotteryEnteredToday(Lottery lottery){
        String currString = Utils.getCurrentTimeByPattern(TIME_PATTERN);
        return TextUtils.equals(currString, getLotteryLastEnteredTime(lottery));
    }
    /**
     * 获取是否进入过抽奖页面
     * 
     * @return
     */
    public String getLotteryLastEnteredTime(Lottery lottery) {
        SharedPreferences sp = context.getSharedPreferences(LOTTERY, Context.MODE_PRIVATE);
        String oldTime = sp.getString("last_entered_time"+lottery.getId(), "never");
        LogUtils.i("lottery", "menu get oldTime:" + oldTime );
        // 判断上次进入与本次进入是否是同一自然天
        return oldTime;
    }

    /**
     * 设置已经进入过抽奖页
     */
    public void setLotteryEntered(Lottery lottery) {
        SharedPreferences sp = context.getSharedPreferences(LOTTERY, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        DateFormat df = new SimpleDateFormat(TIME_PATTERN,Locale.US);
        String currentTime = df.format(new Date());
        LogUtils.i("lottery", "menu set currentTime:" + currentTime);
        editor.putString("last_entered_time"+lottery.getId(), currentTime);
        editor.commit();
    }


    public String triggerLotteryPopWindow(Lottery lottery) {
        SharedPreferences sp = context.getSharedPreferences(LOTTERY, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        String currentTime = Utils.getCurrentTimeByPattern(TIME_PATTERN);
        String oldTime = sp.getString("last_pop_time"+lottery.getId(), "never_trigged");
        editor.putString("last_pop_time"+lottery.getId(), currentTime);
        editor.commit();
        if(TextUtils.equals("never_trigged", oldTime)){
            return oldTime;
        }
        return currentTime;
    }
    /**
     * 活动页弹窗触发器
     * @param lottery
     * @return 若为true则表示可以弹窗，否则不可以弹窗
     * 
     */
    public boolean needPopUp(Lottery lottery) {
        return null!=lottery.getPopWinInfo()
                &&lottery.getFlag()==Lottery.FLAG_ONGOING
                &&checkPopState(lottery);
    }
    public boolean needRedDot(Lottery lottery) {
        if (lottery.getFlag() == Lottery.FLAG_ONGOING) {
            switch(lottery.getPopWinInfo().getFrequency()){
                case LotteryPopWindowInfo.FREQUENCY_EVERY_DAY:
                case LotteryPopWindowInfo.FREQUENCY_EVERY_TIME:
                    return !isLotteryEnteredToday(lottery);
                case LotteryPopWindowInfo.FREQUENCY_ONLY_ONCE:
                    return TextUtils.equals("never", getLotteryLastEnteredTime(lottery));
            }
        }
        return false;
    }

    private boolean checkPopState(Lottery lottery){
        String currentTime = Utils.getCurrentTimeByPattern(TIME_PATTERN);
        switch(lottery.getPopWinInfo().getFrequency()){
            case LotteryPopWindowInfo.FREQUENCY_EVERY_DAY:
                return !TextUtils.equals(currentTime, triggerLotteryPopWindow(lottery));
            case LotteryPopWindowInfo.FREQUENCY_EVERY_TIME:
                triggerLotteryPopWindow(lottery);
                return true;
            case LotteryPopWindowInfo.FREQUENCY_ONLY_ONCE:
                return TextUtils.equals("never_trigged", triggerLotteryPopWindow(lottery));
        }
        return false;
    }
}
